<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row class="cat_opt" :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryinfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodList" border stripe>
        <el-table-column type="index" label="##"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="90"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90"></el-table-column>
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.goods_id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 15, 30, 50]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        queryinfo: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        goodList: [],
        total: 0
      }
    },
    created () {
      this.getGoodsList()
    },
    methods: {
      async getGoodsList () {
        const { data: res } = await this.$http.get('goods', { params: this.queryinfo })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.goodList = res.data.goods
        this.total = res.data.total
      },
      handleSizeChange (newSize) {
        this.queryinfo.pagesize = newSize
        this.getGoodsList()
      },
      handleCurrentChange (newNum) {
        this.queryinfo.pagenum = newNum
        this.getGoodsList()
      },
      // 删除操作
      async removeById (id) {
        const confirmResult = await this.$confirm('确认要删除吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult !== 'confirm') {
          return this.$message({
            type: 'info',
            message: '已取消删除'
          })
        }
        console.log(confirmResult)
        const { data: res } = await this.$http.delete(`goods/${id}`)
        if (res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
        } else {
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        }
      },
      // 跳转到添加页面
      goAddPage () {
        this.$router.push('/goods/add')
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-table {
    margin:15px 0 ;
  }
</style>
